//
//  UserAccountViewModel.swift
//  新浪微播
//
//  Created by lxw on 15/12/18.
//  Copyright © 2015年 lxw. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    //定义用户模型对象的属性
    var userAccount: UserAccount?
    override init() {
            userAccount = UserAccount.loadAccount()
            super.init()
        }
    
    //计算性属性 用户是否登陆
    var userLogin: Bool {
        return userAccount?.access_token != nil
    }
 //用户名
    var userName: String?{
        return userAccount?.name
    }
    
    //用户头像的url
    var headURL: NSURL? {
        let url = NSURL(string: userAccount?.avatar_large ?? "")
        return url
    }
    
   
    func loadAccessToken(code:String,finished:(isSuccess: Bool) ->()){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        let AFN = AFHTTPSessionManager()
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        AFN .POST(urlString, parameters: parameters, progress: { (p) -> Void in
            print(p)
            }, success: { (_, result) -> Void in
                if let dict = result as? [String :AnyObject]{
                    //                        let token = dict["access_token"] as! String
                    //
                    //                        let uid = dict["uid"] as! String
                    //                       self.loadUserInfo(token, uid: uid)
                    let userAccount = UserAccount(dict : dict)
                    self.loadUserInfo(userAccount,finished: finished)
                }
                // print(result)
                
            }) { (_, error) -> Void in
                print(error)
                finished(isSuccess: false)
        }
    }
    //  MARK: 获取用户信息
    private func loadUserInfo(account: UserAccount,finished:(isSuccess: Bool) ->()){
        let urlString = "https://api.weibo.com/2/users/show.json"
        if let token = account.access_token,userId = account.uid{
            let parameters = ["access_token": token,"uid":userId]
            let AFN1  = AFHTTPSessionManager()
            AFN1 .GET(urlString, parameters: parameters, progress: { (_) -> Void in
                //  print(parameters)
                }, success: { (_, result) -> Void in
                    // print(result)
                    if let dict = result{
                        let avatar_large = dict["avatar_large"] as! String
                        let name = dict["name"] as! String
                        //   print(avatar_large,name)
                        account.name=name
                        account.avatar_large=avatar_large
                        // print(account)
                        account.saveAccount()
                        finished(isSuccess: true)
                    }
                }) { (_, error) -> Void in
                    print(error)
                    finished(isSuccess: false)
            }
            
        }
    }

}










